package com.tangqihang229350526.mynote

import android.Manifest
import android.app.DatePickerDialog
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.widget.DatePicker
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.tangqihang229350526.mynote.database.NoteDatabase
import com.tangqihang229350526.mynote.databinding.ActivityAddNoteBinding
import com.tangqihang229350526.mynote.repository.NoteRepository
import java.util.*

class AddNoteActivity : AppCompatActivity() {
    private var binding: ActivityAddNoteBinding? = null
    private var img = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAddNoteBinding.inflate(layoutInflater)
        setContentView(binding!!.root)
        val database = NoteDatabase(this)
        binding!!.btnAdd.setOnClickListener { v ->
            NoteRepository.instance?.insert(database, binding!!, img, this)
        }
        binding!!.ivBack.setOnClickListener { v -> finish() }
        binding!!.tvTime.setOnClickListener { v ->
            val calendar = Calendar.getInstance()
            val year = calendar[Calendar.YEAR]
            val month = calendar[Calendar.MONTH]
            val day = calendar[Calendar.DAY_OF_MONTH]
            val dialog = DatePickerDialog(
                this,
                { view: DatePicker?, year1: Int, month1: Int, dayOfMonth: Int ->
                    binding!!.tvTime.text = year1.toString() + "-" + (month1 + 1) + "-" + dayOfMonth
                }, year, month, day
            )
            dialog.show()
        }
        val launcher = registerForActivityResult(
            RequestPermission()
        ) { result: Boolean ->
            if (result) {
                image
            }
        }
        binding!!.iv.setOnClickListener { view ->
            // 请求权限
            if (Build.VERSION.SDK_INT >= 33) {
                if (checkSelfPermission(Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_DENIED) {
                    launcher.launch(Manifest.permission.READ_MEDIA_IMAGES)
                } else {
                    image
                }
            } else {
                if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                    launcher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
                } else {
                    image
                }
            }
        }
    }

    private val image: Unit
        private get() {
            PictureSelector.create(this)
                .openGallery(SelectMimeType.ofImage())
                .setMaxSelectNum(1)
                .setImageEngine(GlideEngine.createGlideEngine())
                .forResult(object : OnResultCallbackListener<LocalMedia> {
                    override fun onResult(result: ArrayList<LocalMedia>) {
                        if (result.size > 0) {
                            img = result[0].availablePath
                            Glide.with(this@AddNoteActivity).load(img).into(
                                binding!!.iv
                            )
                        }
                    }

                    override fun onCancel() {}
                })
        }
}